#include "qmyprogressbar.h"
#include "../../include/abstractproperty.h"
#include "../../lib/shared/qdatamanager.h"
#include "../../include/qcommonstruct.h"

#include <QVariant>
#include <QMouseEvent>

QProgressBarHost::QProgressBarHost(QWidget *wid):
    QObjectHost(wid)
{

}

int QProgressBarHost::value()
{
    return ((QProgressBar*)m_widget)->value();
}

QMyProgressBar::QMyProgressBar(QWidget *parent):
    QProgressBar(parent),
    m_host(new QProgressBarHost(this))
{
    this->resize(100,30);
    setProperty("className","QProgressBar");
    setProperty("typeName","Progress Bar");
    setProperty("typeIcon",":/images/widgets/progress.png");

    m_host->addProperty("Attribute",QStringList()<<"font"<<"focusPolicy"<<"value"<<"textVisible"
                        <<"maximum"<<"minimum"<<"orientation"<<"invertedAppearance");
    m_host->addProperty("StyleSheet",QStringList()<<"border");
    m_host->addProperty("Signals",QStringList()<<"valueChanged");
    m_host->addUserProperty(QStringList()<<"value");

    connect(this,SIGNAL(valueChanged(int)),this,SLOT(valueChanged_signal(int)));
}

void QMyProgressBar::valueChanged_signal(int value)
{
    QString code=property("valueChanged").toString();
    if(code!="")
    {
        QString para=QString("type=valueChanged,num=%1,_value=%2").arg(1).arg(value);
        m_host->exec(code,para);
    }
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="value")
            {
                manager->setUserData(info.m_valueName,text());

            }
        }
    }
}

void QMyProgressBar::updateProperty(const QString & name)
{
    PropertyList list=property("property_list").value<PropertyList>();
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
    else if(name=="maximum")
    {

        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="value")
            {
                QVariant v=this->property("value");
                if(v!=p->value())
                {
                    p->setValue(v);
                    p->setModified(true);
                }
            }
        }
    }
    else if(name=="minimum")
    {
        foreach(QAbstractProperty *p,list)
        {
            if(p->name()=="value")
            {
                QVariant v=this->property("value");
                if(v!=p->value())
                {
                    p->setValue(v);
                    p->setModified(true);
                }
            }
        }
    }
    m_host->updateStyle(name);
}

void QMyProgressBar::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="valueChanged")
        {
            p->setAttribute("param","valueChanged");
        }
    }
}

void QMyProgressBar::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}
